package com.bluemsun.naturallanguageprocessing.service.impl;

import com.bluemsun.naturallanguageprocessing.common.ServerResponse;
import com.bluemsun.naturallanguageprocessing.dao.BrowseMapper;
import com.bluemsun.naturallanguageprocessing.entity.UserApi;
import com.bluemsun.naturallanguageprocessing.service.IBrowseService;
import com.bluemsun.naturallanguageprocessing.util.ConductRedisUtil;
import com.bluemsun.naturallanguageprocessing.util.RedisUtil;
import com.bluemsun.naturallanguageprocessing.vo.BrowseListVO;
import com.bluemsun.naturallanguageprocessing.vo.BrowseVO;
import com.bluemsun.naturallanguageprocessing.vo.PageVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ：hj
 * @date ：Created in 2021/4/16 14:19
 * @description：浏览
 * @modified By：
 * @version: $
 */
@Service("iBrowseService")
public class IBrowseServiceImpl implements IBrowseService {

    private static Logger logger= LoggerFactory.getLogger(com.bluemsun.naturallanguageprocessing.service.impl.IBrowseServiceImpl.class);

    @Resource
    BrowseMapper browseMapper;

    @Resource
    ConductRedisUtil conductRedisUtil;
    @Override
    public ServerResponse getBrowse(HttpServletRequest request, Integer pageSize, Integer pageNum) {
        UserApi userApi =(UserApi) conductRedisUtil.getUserFromRedis(request.getHeader("token"));
        Integer id = userApi.getId();
        Integer totalCount = browseMapper.getBrowseCount(id);
        if(totalCount==0){
            return ServerResponse.createByErrorMessage("该用户没有浏览记录");
        }
        PageVO<BrowseVO> page = new PageVO(pageNum,pageSize,totalCount);
        List<BrowseVO> list = browseMapper.getBrowse(id,page.getStartIndex(),pageSize);
        //今天的浏览记录
        List<BrowseVO> list1 = new ArrayList<>();
        List<BrowseVO> list2 = new ArrayList<>();
        List<BrowseVO> list3 = new ArrayList<>();
        BrowseListVO browseListVO =new BrowseListVO();
        for(BrowseVO l:list){
            Date date =new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long startDateTime = 0;
            long endDateTime = 0;
            try {
                startDateTime = dateFormat.parse(dateFormat.format(l.getBrowseTime())).getTime();
                endDateTime = dateFormat.parse(dateFormat.format(date)).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
                logger.error("时间转换错误",e);
            }
            double  day= (double) ((endDateTime - startDateTime) / (1000 * 3600 * 24));

            if(day==0){
                list1.add(l);
            }else if(day<=7){
                list2.add(l);
            }else{
                list3.add(l);
            }
        }
        browseListVO.setTodayList(list1);
        browseListVO.setWeekList(list2);
        browseListVO.setEther(list3);
        browseListVO.setAmount(totalCount);
        return ServerResponse.createBySuccess("返回浏览记录",browseListVO);
    }
/**
 * @author     ：hj
 * @date       ：Created in 2021/4/18 16:03
 * @description：删除单条浏览记录
 * @modified By：
 * @version:
 */
    @Override
    public ServerResponse deleteBrowse(Integer id) {
        browseMapper.deleteOnes(id);
        return ServerResponse.createBySuccess("删除成功");
    }
/**
 * @author     ：hj
 * @date       ：Created in 2021/4/18 16:03
 * @description：删除所有浏览记录
 * @modified By：
 * @version:
 */
    @Override
    public ServerResponse deleteAllBrowse(Integer id) {
        //先找到该用户所有的记录
        Integer all=browseMapper.getBrowseCount(id);
        List <BrowseVO>list;
        List <Integer> list1;
        if(all!=null) {
            list = browseMapper.getBrowse(id, 0, all);
            list1 =new ArrayList<Integer>();
            Date date =new Date();
            for(BrowseVO l:list){
                list1.add(l.getId());
            }
            browseMapper.deleteAll(list1);
        }
        return ServerResponse.createBySuccess("删除成功");
    }
}
